﻿
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Windows.Forms ;

using MskEdit.Dxf.Model ;


namespace MskEdit.Dxf.Backend
{
    public static class DxfDocumentIO {
        
        static public string TryGetInputPath( string fallbackValue ) {
        
            var openFileDialog = new OpenFileDialog {
                DefaultExt       = "dfx",
                Filter           = "Dxf Files|*.dfx",
                InitialDirectory = "", // MskSession.InputDirectory = "",
                RestoreDirectory = true,
                Title            = "Open input Dxf File"
            } ;
            
            openFileDialog.ShowDialog() ;

            if ( string.IsNullOrEmpty( openFileDialog.FileName ) )
                return fallbackValue ;
                
            return openFileDialog.FileName ;
        }


        static public string TryGetOutputPath( string fallbackValue ) {
        
            var saveFileDialog = new SaveFileDialog {
                DefaultExt       = "dxf",
                Filter           = "Dxf Files|*.dxf",
                InitialDirectory = "", // MskSession.OutputDirectory = "",
                RestoreDirectory = true,
                Title            = "Open output DXF File"
            } ;
            
            saveFileDialog.ShowDialog() ;

            if ( string.IsNullOrEmpty( saveFileDialog.FileName ) )
                return fallbackValue ;
                
            return saveFileDialog.FileName ;
        }
        
        
        public static DxfDocument TryRead( FileInfo dxfFileInfo, DxfDocument fallbackValue ) {    
                     
            if ( ! dxfFileInfo.Exists )
                return fallbackValue ;
                
            if ( string.IsNullOrEmpty( dxfFileInfo.Extension ) )
                return fallbackValue ;

            if ( ! dxfFileInfo.Extension.ToLower().Equals( ".dxf" ) )
                return fallbackValue ;

            string [] allLines = File.ReadAllLines( dxfFileInfo.FullName ) ;
            for ( int i = 0 ; i < allLines.Length ; ++i ) {
                allLines[i] = allLines[i].Trim() ;
            }
            
            string documentName = dxfFileInfo.Name.Substring( 0, dxfFileInfo.Name.Length - 4 ) ;
            
            DxfDocument dxfDocument = DxfDocument.TryFrom( documentName, allLines, null ) ;
            if ( dxfDocument == null )
                return fallbackValue ;

            return dxfDocument ;                 
        }
        
        
        public static DxfDocument[] GetDxfDocuments( DirectoryInfo directoryInfo ) {
        
            List<DxfDocument> dxfDocuments = new List<DxfDocument>() ;
            
            foreach ( FileInfo dxfFileInfo in directoryInfo.GetFiles( "*.dxf" ) ) {
            
                DxfDocument dxfDocument = DxfDocumentIO.TryRead( dxfFileInfo, null ) ;
                if (dxfDocument == null )
                    continue ;
                    
                dxfDocuments.Add( dxfDocument ) ;
            }
            
            return dxfDocuments.ToArray() ;
        }
        
    }
}
